import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/res/style/color_styles.dart';
import 'package:flutter/material.dart';

///旋转图标
class RotateArrowWidget extends StatefulWidget {
  final bool isRotate;
  final Function(bool) onChange;

  const RotateArrowWidget(
      {super.key, required this.isRotate, required this.onChange});

  @override
  State<RotateArrowWidget> createState() => RotateArrowWidgetState();
}

class RotateArrowWidgetState extends State<RotateArrowWidget>
    with SingleTickerProviderStateMixin {
  //动画控制器
  late AnimationController _animationController;

  //图标变化取值
  late Animation<double> _animateIcon;
  late bool isRotated;

  @override
  void initState() {
    super.initState();
    isRotated = widget.isRotate;
    //初始化动画控制器
    _animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 300));
    //添加动画监听
    _animationController.addListener(() {
      setState(() {});
    });
    //Tween结合_animationController，使300毫秒内执行一个从0.0到0.1的变换过程
    _animateIcon =
        Tween<double>(begin: 0.0, end: 1.0).animate(_animationController);
  }

  void rotate() {
    if (!isRotated) {
      _animationController.forward(); //展开动画
    } else {
      _animationController.reverse(); //收回动画
    }
    isRotated = !isRotated;
    widget.onChange(isRotated);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (!isRotated) {
          _animationController.forward(); //展开动画
        } else {
          _animationController.reverse(); //收回动画
        }
        isRotated = !isRotated;
        widget.onChange(isRotated);
      },
      child: AnimatedBuilder(
          animation: _animateIcon,
          builder: (context, child) {
            return Transform.rotate(
              angle: -_animateIcon.value * 1 * 3.141592,
              child: Icon(
                Icons.keyboard_arrow_down_outlined,
                size: 44.rpx,
                color: ColorStyles.color_333333,
              ),
            );
          }),
    );
  }
}
